/*
 * InputMatrixHolder.cpp
 *
 *  Created on: Oct 17, 2012
 *      Author: David Kadlecek, Ales Fiser
 */

#include "InputMatrixHolder.h"

InputMatrixHolder::InputMatrixHolder() {
	matrix = NULL;
	numberOfNodes = -1;
}

void InputMatrixHolder::readFromFile(string pathToFile) {
	ifstream myfile(pathToFile.c_str());
	if (myfile.is_open()) {
		myfile >> numberOfNodes;
		initNewMatrix(numberOfNodes);
		string line;
		int index = 0;
		while (index < numberOfNodes * numberOfNodes) {
			getline(myfile, line);
			int tmp;
			for (size_t i = 0; i < line.length(); i++) {
				tmp = line.at(i);
				matrix[index] = (tmp - 48) == 0 ? false : true;
				index++;
			}
		}
	} else {
		cout << "Unable to open file: " << pathToFile << endl;
	}
	calculateNodeDegrees();
	myfile.close();
}

void InputMatrixHolder::calculateNodeDegrees() {
	int degree, i, j;
	for (i = 0; i < numberOfNodes; ++i) {
		degree = 0;
		for (j = 0; j < numberOfNodes; ++j) {
			if (getVlaue(i, j)) degree++;
		}
		nodeDegree[i] = degree;
	}

	if (logCalculatedNodesDegrees) {
		for (i = 0; i < numberOfNodes; ++i) {
			cout << nodeDegree[i] << " ";
		}
		cout << endl;
	}
}

int InputMatrixHolder::getNodeDegree(int node) {
	return nodeDegree[node];
}

void InputMatrixHolder::initNewMatrix(int numOfNodes) {
	if (matrix != NULL) {
		delete matrix;
	}
	matrix = new bool[numOfNodes * numOfNodes];
	nodeDegree = new int[numOfNodes];
}

InputMatrixHolder::~InputMatrixHolder() {
	if (matrix != NULL) {
		delete matrix;
	}
}

int InputMatrixHolder::getNumberOfNodes() {
	return numberOfNodes;
}

bool* InputMatrixHolder::getMatrix() {
	return matrix;
}

bool InputMatrixHolder::getVlaue(int row, int column) {
	return matrix[(row * numberOfNodes) + column];
}

